package com.pingao.mp.modular.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pingao.mp.modular.sys.entity.Role;
import com.pingao.mp.modular.sys.entity.User;
import com.pingao.mp.modular.sys.entity.UserRoles;
import com.pingao.mp.modular.sys.mapper.RoleMapper;
import com.pingao.mp.modular.sys.mapper.UserMapper;
import com.pingao.mp.modular.sys.mapper.UserRolesMapper;
import com.pingao.mp.modular.sys.service.UserRolesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;

import java.time.LocalDateTime;
import java.util.List;
@Service
public class UserRolesServiceImpl extends ServiceImpl<UserRolesMapper, UserRoles> implements UserRolesService{
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    DataSourceTransactionManager dataSourceTransactionManager;
    @Autowired
    TransactionDefinition transactionDefinition;

    @Override
    public List<Role> listByUserId(Long userId) {
        User user = userMapper.selectByIdJoin(userId);
//        List<Role> roles = roleMapper.selectList(new QueryWrapper<Role>().isNull(UserRoles.COL_DELETE_TIME).ne(Role.COL_ID, user.getRoleId()));
        List<Role> roles = roleMapper.selectList(new QueryWrapper<Role>().isNull(UserRoles.COL_DELETE_TIME));
        for (Role role : roles) {
            if (role.getId().equals(user.getRoleId())) {
                role.setHasPermission(true);
                continue;
            }
            Integer count = baseMapper.selectCount(new QueryWrapper<UserRoles>().eq(UserRoles.COL_USER_ID, user.getId()).eq(UserRoles.COL_ROLE_ID, role.getId()));
            if (count > 0) {
                role.setHasPermission(true);//userId用户拥有该role
            }
        }
        return roles;
    }

    @Override
    public void saveUserRoles(Long userId, List<Long> roleIds) {
        TransactionStatus transactionStatus = null;
        try {
            transactionStatus = dataSourceTransactionManager.getTransaction(transactionDefinition);//开启一个新的事务
            baseMapper.delete(new QueryWrapper<UserRoles>().eq(UserRoles.COL_USER_ID, userId));//删除用户所有的额外角色
            if (roleIds != null && roleIds.size() > 0) {
                for (Long roleId : roleIds) {
                    UserRoles roles = new UserRoles();
                    roles.setUserId(userId);
                    roles.setRoleId(roleId);
                    roles.setCreateTime(LocalDateTime.now());
                    roles.insertOrUpdate();
                }
            }
            dataSourceTransactionManager.commit(transactionStatus);//提交
        } catch (Exception e) {
            if (transactionStatus != null) {
                dataSourceTransactionManager.rollback(transactionStatus);//最好是放在catch 里面,防止程序异常而事务一直卡在哪里未提交
            }
        }
    }
}
